package com.dotandmedia.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.dotmedia.dboperations.AdTemplateDatabaseOperations;
import com.dotmedia.dboperations.AdvertiserDatabaseOperations;
import com.dotmedia.entities.AdTemplate;
import com.dotmedia.entities.Advertiser;
import com.opensymphony.xwork2.ActionSupport;

public class DisplayAddNewAdAction extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware {

	private static Logger logger = Logger.getLogger("dotmedia.action");

	protected Map session;
	protected HttpServletRequest request;
	protected HttpServletResponse response;

	@Override
	public void setServletResponse(HttpServletResponse resp) {
		this.response = resp;
	}

	@Override
	public void setServletRequest(HttpServletRequest req) {
		this.request = req;
	}

	@Override
	public void setSession(Map session) {
		this.session = session;
	}

	public String displayAddNewAd() {
		logger.info("IN action DisplayAddNewAdAction");
		// get all advertisers
		List<Advertiser> allAdvertisers = new AdvertiserDatabaseOperations().getAllAdvertiserEntities();
		String advertiserAutocompleteString = "";
		for (int allAdvertisersIndex = 0; allAdvertisersIndex < allAdvertisers.size(); allAdvertisersIndex++) {
			Advertiser currentAdvertiser = allAdvertisers.get(allAdvertisersIndex);
			String currentAutocompleteString = "\"" + currentAdvertiser.getDescription() + " [" + currentAdvertiser.getAdvertiserRefCode() + "] ["
					+ currentAdvertiser.getAdvertiserId() + "]\"";
			advertiserAutocompleteString += currentAutocompleteString;
			if (allAdvertisersIndex != (allAdvertisers.size() - 1)) {
				advertiserAutocompleteString += ",";
			}
		}
		request.setAttribute("advertiserAutocompleteString", advertiserAutocompleteString);
		//get all ad templates
		List<AdTemplate> allAdTemplate = new AdTemplateDatabaseOperations().getAllAdTemplateEntities();
		Map<String, String> adTplIdVsAdTplCode = new HashMap<String, String>();
		for (int allAdTemplateIndex = 0; allAdTemplateIndex < allAdTemplate.size(); allAdTemplateIndex++) {
			AdTemplate currentAdTemplate = allAdTemplate.get(allAdTemplateIndex);
			adTplIdVsAdTplCode.put("" + currentAdTemplate.getAdtplId(), currentAdTemplate.getCode());
		}
		request.setAttribute("adTplIdVsCode", adTplIdVsAdTplCode);
		logger.info("OUT action DisplayAddNewAdAction");
		return SUCCESS;
	}

}
